package com.itheima.prize.api.action;

import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.service.CardUserService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PasswordUtil;
import com.itheima.prize.commons.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.itheima.prize.commons.config.RedisKeys.USERLOGINTIMES;

@RestController
@RequestMapping(value = "/api")
@Api(tags = {"登录模块"})
public class LoginController {
    @Autowired
    private CardUserService userService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 用户登入
     * @param request
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="account",value = "用户名",required = true),
            @ApiImplicitParam(name="password",value = "密码",required = true)
    })
    public ApiResult login(HttpServletRequest request, @RequestParam String account,@RequestParam String password) {
        //1.查询用户信息
        CardUser user = userService.getUserByAccount(account);
        //2.判断用户是否存在
        if(user==null){
            return new ApiResult<>(0,"账户名或密码错误",null);
        }
        //3.校验密码
        String loginKey = USERLOGINTIMES + account;
        // 3.1判断密码错误次数是否满5次
        Object value = redisUtil.get(loginKey);
        int count = 0;
        if(value!=null) {
            count = Integer.parseInt(value.toString());
        }
        if(count<5) {
            //判断密码是否正确
            if (!PasswordUtil.encodePassword(password).equals(user.getPasswd())) {
                long i = redisUtil.incr(loginKey, 1);
                redisUtil.expire(loginKey, 300);
                if (i == 5) {
                    return new ApiResult<>(0, "密码错误5次，请5分钟后再登录", null);
                }
                return new ApiResult<>(0, "账户名或密码错误", null);
            }
        }else {
            //错误次数大于5次，返回错误信息
            return new ApiResult<>(0, "密码错误5次，请5分钟后再登录", null);
        }
        //5.登入成功
        // 5.1删除redis密码错误计数器key
        redisUtil.del(loginKey);
        // 5.2将用户信息存入session，返回用户信息
        user.setIdcard(null);
        user.setPasswd(null);
        request.getSession().setAttribute("user",user);
        return new ApiResult<>(1, "登入成功", user);
    }

    /**
     * 退出登入
     * @param request
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation(value = "退出")
    public ApiResult logout(HttpServletRequest request) {
        //删除session中的user信息
        request.getSession().removeAttribute("user");
        return new ApiResult<>(1, "退出成功", null);
    }

}